<template>
  <div class="upLoadTop">
    <div class="ui container">
      <div class="ui grid">
        <div class="sixteen wide column">
          <div class="ui segment">
            <div style="margin:1rem auto">
              <!-- 选择是否加密 -->
              <el-radio-group v-model="typeChose">
                <el-radio-button label="开放"></el-radio-button>
                <el-radio-button label="加密"></el-radio-button>
              </el-radio-group>
            </div>
            <!-- 文件选择 -->
            <el-upload class="upload-demo" ref="upload" :auto-upload="false" :on-change="onchange"
              :before-remove="beforeRemove" drag action="#" :on-exceed="handlExceed" :limit="1">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                将文件拖到此处，或<em>点击上传</em>
              </div>
              <div class="el-upload__tip" slot="tip">
                上传文件不超过50MB
              </div>
            </el-upload>
            <div style="margin: 1rem auto;padding:0 4rem;">
              <!-- 文件描述 -->
              <el-input type="textarea" placeholder="请输入对文件的描述..." v-model="description" maxlength="255"
                show-word-limit>
              </el-input>
            </div>
            <div style="margin: 1rem auto;max-width: 30rem;">
              <!-- 文件加密 -->
              <el-input v-if="this.typeChose == '加密'" placeholder="请输入加密密码..." v-model="pwd" show-password></el-input>
            </div>
          </div>
        </div>
      </div>
      <div style="margin-top:2em;!important">
        <sui-button primary @click="myUploadFile">提交文件</sui-button>
      </div>
      <el-dialog :visible.sync="dialogVisible" type="warning" top="20rem" width="30%" :close-on-click-modal="false"
        :close-on-press-escape="false" :show-close="false">
        <div slot="title" class="dialog-title">
          <i class="el-icon-warning"></i>
          <span class="title-text">Warning</span>
        </div>
        <p style="margin-top: 0; font-size: 16px">
          请合法上传文件、请勿上传违反互联网法律的文件。保护好个人隐私、文明上网！
        </p>
        <span slot="footer" class="dialog-footer">
          <el-checkbox style="float: left; line-height: 40px" v-model="checked">不再提醒</el-checkbox>
          <el-button type="primary" @click="func()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>

export default {
  inject: ['reload'],
  data() {
    return {
      typeChose: '开放',          //选择是否加密
      currentFile: null,          //当前上传的文件
      description: '',   //文件描述
      pwd: '',              //加密文件密码
      dialogVisible: false,   //提醒框
      checked: false,      //是否提醒warning
    }
  },
  methods: {
    //关闭提示
    func() {
      if (this.checked == false) {
        this.dialogVisible = false;
        return;
      } else {
        sessionStorage.setItem("show", true);
        this.dialogVisible = false;
      }
    },
    //是否展示提示符
    checkshow() {
      let tmp = sessionStorage.getItem("show");
      if (!tmp || tmp == false) {
      this.dialogVisible = true;
      } else {
        this.dialogVisible = false;
      }
    },
    onchange(file, fileList) {
      // 限制上传文件的大小
      const isLt = file.size / 1024 / 1024 / 50 <= 1;
      if (!isLt) {
        this.$message.error("上传文件大小不得大于50MB!");
        this.$refs.upload.clearFiles();
      } else {
        this.currentFile = file;
      }
      return;
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定取消上传 ${file.name}？`, '提示');
    },
    myUploadFile() {
      const loading = this.$loading({
        lock: true,
        text: '文件上传中',
      });
      var formData = new FormData();
      formData.append("file", this.currentFile.raw);
      formData.append("description", this.description);
      if (this.pwd != '')
        formData.append("code", this.pwd);
      this.$https.post('/user/upload', formData).then((resp) => {
        this.reload();
        loading.close();
        if (resp.data.code == 200) {
          this.$refs.upload.clearFiles();
          this.description = '';
          this.pwd = '';
          this.$message.success(resp.data.data);
        } else {
          this.$message.info(resp.data.message);
        }
      })
    },
    handlExceed() {
      this.$message.error("只能选择一个文件进行上传");
    },
  },
  created() {
    // this.messageWarning();
    this.checkshow();
  }
}
</script>

<style scoped>
.upLoadTop {
  padding-top: 0.5em !important;
  padding-bottom: 0.5em !important;
}
.dialog-title {
    font-size: 1.5rem;
    font-weight: 300;
  }
  .dialog-title i {
    color: #e6a23c;
    margin-right:0.5rem;
    font-size: 2rem;
  }
</style>